﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Universe.HpglParser
{
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using NDesk.Options;
    using VectorImage;

    class Program
    {
        static void Main(string[] args)
        {

            string input = null;
            string output = null;
            string fullTypeName = null;
            bool help = false;
            bool nologo = false;
            
            var p = new OptionSet()
            {
                {"i|input=", "Input HPGL, required", v => input = v},
                {"o|output=", "Output C# source", v => output = v},
                {"t|type=", "Full C# class type name including namespace, required", v => fullTypeName = v},
                {"h|?|help", "Display this help", v => help = v != null},
                {"n|nologo", "Hide logo", v => nologo = v!= null }
            };

            List<string> extra = p.Parse(args);
            
            if (!nologo)
            {
                Console.WriteLine("HP/GL to C# source code coverter. (c) Devizer 2012");
                Console.WriteLine();
            }

            // Debugger.Break();
            if (input != null && output != null && fullTypeName != null)
            {
                using (var streamInput = new FileStream(input, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (var textReader = new StreamReader(streamInput, Encoding.GetEncoding(1252)))
                using(var streamOut = new FileStream(output, FileMode.Create, FileAccess.Write, FileShare.Read))
                using (var writer = new StreamWriter(streamOut, Encoding.UTF8))
                {
                    var lines = HpglReader.Parse(textReader).ToArray();
                    SharpWriter.Write(writer, lines, fullTypeName);

                    SimpleImage si = new SimpleImage(lines);
                    var scale = Math.Max(si.Bounds.Width, si.Bounds.Height) / 4000m;
                    var debugSize = new Size((int) (si.Bounds.Width/scale), (int) (si.Bounds.Height/scale));
                    Bitmap bmp = new Bitmap(debugSize.Width, debugSize.Height, PixelFormat.Format32bppArgb);
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        g.SmoothingMode = SmoothingMode.AntiAlias;
                        var sz = debugSize;
                        // g.FillRectangle(SystemBrushes.Window, 0, 0, sz.Width, sz.Height);
                        var img = si;
                        float sx = 1f * (sz.Width - 2) / img.Bounds.Right;
                        float sy = 1f * (sz.Height - 2) / img.Bounds.Bottom;
                        g.ScaleTransform(sx, sy);
                        Pen pe = new Pen(Color.Black, 3.0f);
                        var path = Renderer.GetGraphicsPath(img.Lines);
                        // Renderer.Render(e.Graphics, img.Lines, p);
                        g.FillPath(new SolidBrush(Color.Silver), path);
                        g.DrawPath(pe, path);
                    }

                    bmp.Save(output + ".png", ImageFormat.Png);


                }
            }
        }
    }
}
